//
//  MM_CodeResultListVC.m
//  MallManage_IOS
//
//  Created by 张浩 on 2023/10/16.
//

#import "MM_HomeSkuDdetailGiveawayListVC.h"
#import "MM_HomeSkuDdetailGiveawayListCell.h"
#import "MM_HomeSkuDdetailGiveawaySectionModel.h"
#import "MM_HomeSkuDdetailGiveawayHeaderSectionView.h"

@interface MM_HomeSkuDdetailGiveawayListVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger page_num;
@property (nonatomic, strong) UIView *headerSectionView;

@end

@implementation MM_HomeSkuDdetailGiveawayListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleView.title = @"赠品列表";
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.headerSectionView];
    self.page_num = 1;
    [self setupRefresh];
    
    [self showHudInView:self.view hint:@"正在加载数据..."];
    [self loadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    MM_HomeSkuDdetailGiveawaySectionModel *sectionModel = self.dataArr[section];
    return sectionModel.detailList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MM_HomeSkuDdetailGiveawaySectionModel *sectionModel = self.dataArr[indexPath.section];
    
    MM_HomeSkuDdetailGiveawayListCell *cell = [MM_HomeSkuDdetailGiveawayListCell cellWithTableView:tableView];
    cell.model = sectionModel.detailList[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (self.dataArr.count > 0) {
        
        MM_HomeSkuDdetailGiveawaySectionModel *sectionModel = self.dataArr[section];
        
        MM_HomeSkuDdetailGiveawayHeaderSectionView *headetView = [MM_HomeSkuDdetailGiveawayHeaderSectionView headerWithTableView:tableView];
        headetView.sectionModel = sectionModel;
        return headetView;
    } else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (self.dataArr.count > 0) {
        return UITableViewAutomaticDimension;
    } else {
        return 0.01;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (void)loadData {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    parmDic[@"pageIndex"] = @(self.page_num).description;
    parmDic[@"pageSize"] = @"10";
    parmDic[@"sku_id"] = self.sku_id;

    [MM_Http postRequestURLStr:KMM_Gift_List withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        [self hideHud];
        [self endRefersh];
               
        NSString *total_count = [requestDic[@"totals"] description];
        
        NSArray *dataArr = [MM_HomeSkuDdetailGiveawaySectionModel mj_objectArrayWithKeyValuesArray:requestDic[@"rows"]];
        
        for (int i=0; i<dataArr.count; i++) {
            MM_HomeSkuDdetailGiveawaySectionModel *sectionModel = dataArr[i];
             sectionModel.sectionName = [NSString stringWithFormat:@"赠品套餐%@",[Custom_ZHTool translationChinese:@(i + 1).description]];
        }
        
        if (self.page_num <= 1) {
            [self.dataArr removeAllObjects];
        }
        
        [self.dataArr addObjectsFrom_MyArr:dataArr];    
        [self.tableView reloadData];
        
        if (self.dataArr.count <= 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
//                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            if (total_count.integerValue == self.dataArr.count && self.dataArr.count > 0) {
                
                if (self.dataArr.count > 10) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }
                
            }else {
                [self.tableView.mj_footer endRefreshing];
            }
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        [self endRefersh];
        [self hideHud];
        [self showStateText:errorInfo];
    }];
}

- (UITableView *)tableView {
        
    if (_tableView == nil) {
        
        _tableView = [UITableView estimatedLayoutTableview:CGRectMake(0, self.headerSectionView.MM_Max_Y, ScreenW, ScreenH - NaviBarHeight - HomeIndicatorHeight - 40) tableviewStyle:UITableViewStyleGrouped delegateAndDataSource:self estimatedSectionHeader:YES estimatedSectionFooter:YES];
        _tableView.backgroundColor = MM_ColorUtil(@"#f7f7f7");
    }
    return _tableView;
}

- (UIView *)headerSectionView {
    
    if (_headerSectionView == nil) {
        
        _headerSectionView = [[UIView alloc] initWithFrame:CGRectMake(0, NaviBarHeight, ScreenW, 40)];
        
        NSArray *titleArr = @[@"商品名称",@"商品规格",@"规格编码",@"数量",];
        CGFloat titleW = ScreenW /titleArr.count;
                        
        for (int i=0; i<titleArr.count; i++) {
            
            UIView *titleView = [[UIView alloc] init];
            titleView.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
            [_headerSectionView addSubview:titleView];
            
            [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
               
                make.left.equalTo(titleW *i);
                make.top.equalTo(_headerSectionView);
                make.width.equalTo(titleW);
                make.height.equalTo(40);
            }];
                        
            UILabel *nameLabel = [Custom_ZHTool uiLabelCreate:titleArr[i] Font:FONT(13) TextColor:[ColorUtil colorWithHexadecimal:@"#333333"]];
            nameLabel.text = titleArr[i];
            nameLabel.font = FONT_BOLD(12);
            nameLabel.textAlignment = NSTextAlignmentCenter;
            [titleView addSubview:nameLabel];
            
            [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
    //                if (i == 0) {
    //                    make.left.equalTo(12);
    //                }else {
    //                    make.left.equalTo(10);
    //                }
    //
                make.centerX.centerY.equalTo(titleView);
            }];
            
    //            UIView *lineView = [[UIView alloc] init];
    //            lineView.backgroundColor = [ColorUtil colorWithHexadecimal:@"#DDDDDD"];
    //            [titleView addSubview:lineView];
    //
    //            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
    //
    //                make.right.top.equalTo(titleView);
    //    //            make.top.equalTo(self.scrollView);
    //                make.width.equalTo(0.5);
    //                make.height.equalTo(40);
    //            }];

        }

    }
    return _headerSectionView;
}

- (NSMutableArray *)dataArr {
    
    if (_dataArr == nil) {
         
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

- (void)setupRefresh {
    
    WS(weakSelf);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //请求数据
        weakSelf.page_num = 1;
        [weakSelf loadData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        weakSelf.page_num += 1;
        [weakSelf loadData];
    }];
}

- (void)endRefersh {
    if (self.tableView.mj_header) {
        [self.tableView.mj_header endRefreshing];
    }
    if (self.tableView.mj_footer) {
        [self.tableView.mj_footer endRefreshing];
    }
}


@end
 
